package edu.neu.cs6515.xml.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import edu.neu.cs6515.xml.Score;

public class ScoreConverter implements Converter {

    @Override
    public boolean canConvert(final Class type) {
        return Score.class.equals(type);
    }

    @Override
    public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
        Score s = (Score) source;
        writer.addAttribute("orange", String.valueOf(s.getOrange()));
        writer.addAttribute("red", String.valueOf(s.getRed()));
        writer.addAttribute("green", String.valueOf(s.getGreen()));
        writer.addAttribute("yellow", String.valueOf(s.getYellow()));
        writer.addAttribute("purple", String.valueOf(s.getPurple()));
        writer.addAttribute("blue", String.valueOf(s.getBlue()));
    }

    @Override
    public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
        int orange = Integer.valueOf(reader.getAttribute("orange"));
        int red = Integer.valueOf(reader.getAttribute("red"));
        int green = Integer.valueOf(reader.getAttribute("green"));
        int yellow = Integer.valueOf(reader.getAttribute("yellow"));
        int purple = Integer.valueOf(reader.getAttribute("purple"));
        int blue = Integer.valueOf(reader.getAttribute("blue"));
        Score s = new Score(orange, red, green, yellow, purple, blue);
        return s;
    }
}